﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Empires.RAL.Access;
using Empires.CRL.DataContract;
using Empires.RAL.ORM.CompanyData;

namespace Empires.RAL.Pump
{
    public class CompanyBusinessDataPump : ICompanyBusinessAccess
    {
        public List<DefaultDataPack> GetUtilsDetails(string copName, int type)
        {
            List<DefaultDataPack> list = new List<DefaultDataPack>();
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_utils
                              where c.c_nvc_cpname == copName && c.c_i_type == type && !c.c_b_isDelete
                              select new
                              {
                                  c.c_dt_pbdate,
                                  c.c_nvc_author,
                                  c.c_nvc_content,
                                  c.c_nvc_title
                              }).FirstOrDefault();
                if (result != null)
                {
                    list.Add(new DefaultDataPack()
                    {
                        DATE = result.c_dt_pbdate.ToShortDateString(),
                        AUTHOR = result.c_nvc_author,
                        CONTENT = result.c_nvc_content,
                        TITLE = result.c_nvc_title
                    });
                }
                else
                {
                    list.Add(new DefaultDataPack());
                }
                return list;
            }
        }

        public List<DefaultDataPack> GetBusinessDetail(string copname, int bid)
        {       
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                List<DefaultDataPack> list = new List<DefaultDataPack>();
                if (bid == -1)
                {
                    var item = (from c in ctx.tb_business
                                where c.c_vc_cpname == copname && !c.c_b_isdelete
                                select new { c.c_i_id, c.c_nvc_content, c.c_nvc_title,c.c_i_type })
                                .FirstOrDefault();
                    if (item != null)
                    {
                        list.Add(new DefaultDataPack()
                        {
                            ID = item.c_i_id,
                            CONTENT = item.c_nvc_content,
                            TITLE = item.c_nvc_title,
                            TYPE = item.c_i_type
                        });
                    }
                    return list;
                }
                var result = (from c in ctx.tb_business
                              where c.c_vc_cpname == copname && c.c_i_id == bid && !c.c_b_isdelete
                              select new { c.c_i_id, c.c_nvc_content, c.c_nvc_title,c.c_i_type })
                              .ToList().Select(c => new DefaultDataPack()
                              {
                                  ID = c.c_i_id,
                                  CONTENT = c.c_nvc_content,
                                  TITLE = c.c_nvc_title,
                                  TYPE = c.c_i_type
                              }).ToList();
                return result;
            }
        }

        public List<DefaultDataPack> GetBusinessList(string copname, int page, int size, int type, int subject)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var result = (from c in ctx.tb_business
                              where c.c_vc_cpname == copname && !c.c_b_isdelete
                              orderby c.c_i_id descending
                              select new { c.c_i_id, c.c_i_subject, c.c_i_type, c.c_nvc_title })
                             .Skip(page * size)
                             .Take(size);
                if (type != -1)
                {
                    result = result.Where(c => c.c_i_type == type);
                }
                if (subject != -1)
                {
                    result = result.Where(c => c.c_i_subject == subject);
                }
                List<DefaultDataPack> list = result.ToList().Select(c =>
                    new DefaultDataPack()
                    {
                        ID = c.c_i_id,
                        TITLE = c.c_nvc_title,
                        TYPE = c.c_i_type,
                        SUBJECT = c.c_i_subject
                    }).ToList();

                //var result = (from c in ctx.tb_business
                //              where c.c_vc_cpname == copname && !c.c_b_isdelete &&
                //              select new { c.c_i_id, c.c_vc_type, c.c_nvc_title })
                //             .Select(c => new DefaultDataPack()
                //             {
                //                 ID = c.c_i_id,
                //                 TYPE = c.c_vc_type,
                //                 TITLE = c.c_nvc_title
                //             }).ToList();
                //List<DefaultDataPack> list = result.GroupBy(c => c.ID)
                //    .Select(c => c.FirstOrDefault())
                //    .ToList();
                return list;
            }
        }

        public int GetBusinessItemCount(string copname, int type, int subject)
        {
            //using (mysql_companyEntities ctx = new mysql_companyEntities())
            //{
            //    var result = from c in ctx.tb_business
            //                 where c.c_vc_cpname == copname && !c.c_b_isdelete
            //                 select c;
            //    if (type != -1)
            //    {
            //        result = result.Where(c => c.c_i_type == type);
            //    }
            //    if (subject != -1)
            //    {
            //        result = result.Where(c => c.c_i_subject == subject);
            //    }
            //    if (isMain != -1)
            //    {
            //        result = result.Where(c => c.c_b_isMain == (isMain == 1));
            //    }
            //    return result.Count();
            //}
            return 1;
        }
        
        public int BusinessItemDelete(string copname, int bid)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var item = (from c in ctx.tb_business
                            where c.c_vc_cpname == copname && c.c_i_id == bid
                            select c).FirstOrDefault();
                if (item != null)
                {
                    item.c_b_isdelete = true;
                    return ctx.SaveChanges();
                }
                return 0;
            }
        }

        public bool EditBusinessItem(string copname, BusinessDataPack model)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                var item = (from c in ctx.tb_business
                            where c.c_vc_cpname == copname && c.c_i_id == model.Bid
                            select c).FirstOrDefault();
                if (item != null)
                {
                    item.c_nvc_title = model.Title;
                    item.c_nvc_content = model.Content;
                    return ctx.SaveChanges() == 1;
                }
                return false;
            }
        }

        public int AddBusinessItem(string copname, BusinessDataPack businessDataPack)
        {
            using (mysql_companyEntities ctx = new mysql_companyEntities())
            {
                ctx.tb_business.AddObject(new tb_business() { 
                    c_nvc_title = businessDataPack.Title,
                    c_b_isdelete = false,
                    c_i_type = businessDataPack.Type,
                    c_nvc_content = businessDataPack.Content,
                    c_vc_cpname = copname
                });
                return ctx.SaveChanges();
            }
        }


        public void UpdateBusiness(string copname, BusinessDataPack businessDataPack)
        {
            //using (mysql_companyEntities ctx = new mysql_companyEntities())
            //{
            //    var item = (from c in ctx.tb_business
            //                where c.c_vc_cpname == copname && c.c_i_id == businessDataPack.Bid
            //                select c).FirstOrDefault();
            //    item.c_b_isMain = businessDataPack.IsMain;
            //    item.c_i_refences = businessDataPack.RefencesID;
            //    item.c_nvc_content = businessDataPack.Content;
            //    item.c_nvc_title = businessDataPack.Title;
            //    ctx.SaveChanges();
            //}
        }
    }
}
